package com.anbao.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import com.anbao.action.MessageReadAction;

public class SoketClient {


	  //客户端的socket

	  public   Socket socket;    

	  public  InputStream in;
	  
	  public  OutputStream out;

	  public  boolean runState = false;
	  
	  private SeocketServer seocketServer;
	  
	  
	  private static SoketClient soketClient =new  SoketClient();
	  
	  private   MessageReadAction service;
	  
	  public SoketClient() {
		  service = new MessageReadAction();
		  
	}
	  public static synchronized SoketClient  getSoketClient(){
		  
		  
		  return soketClient;
	  }
	  
	  public void setSeocketServer(SeocketServer seocketServer){
		  
		  this.seocketServer = seocketServer;
	  }
	  
	  
	  public boolean getRunState(){
		  
		  
		  return runState;
	  }
	  
	  public  void  connectServer(String ip,int port) 

	  {

	    try 

	    {

	      socket = new Socket(ip, port);
	      runState = true;
	    }

	    catch(IOException e) 

	    {
	    	  runState = false;
	          e.printStackTrace();

	    }

	    //实例化IO对象

	try 

	    {    

	      in = socket.getInputStream(); 

	       out = socket.getOutputStream();

	        //开启线程

	       new Thread(new Runnable() {
			
			@Override
			public void run() {

				
				while(runState){
					
					try {
						readMessage();
					} catch (IOException e1) {
						 runState = false;
						e1.printStackTrace();
					}
					
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						 runState = false;
						e.printStackTrace();
					}
					
				}
				
			}
		}).start();

	     } 

	     catch(IOException e) 

	     {

	        //出现异常，关闭socket 

	          try 

	          {
	        	  runState = false;
	            socket.close();

	        } 

	          catch(IOException e2) 

	          {
	        	  runState = false;
	              e2.printStackTrace();       

	          }

	     }

	  }  

	  
	  public  boolean sendMessage(byte[] data){
		  
		  try {
			out.write(data);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
		  
	  }
	  
	  
	  /**
	   * 关闭socket
	   * @return
	   */
	 public   boolean closeSocket(){
		 
		 try {
			 
			
			 runState = false;
			 
			 if(out!=null){
				 
					out.close();
			 }
			 if(in!=null){
				 
					in.close();
			 }
			 
			 
			 if(socket!=null){
				 
				 socket.close();
				 
			 }
		
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		 
		 
		return true;
		 
	 }
	  
	  /**
	   * 读取消息
	   * @throws IOException
	   */
	protected  void readMessage() throws IOException {
	
		byte[] bytes = new byte[1024];
		List<Byte> data = new ArrayList<Byte>();
		
		int num  = 0;
		if((num = in.read(bytes))==-1){
			
			return;
		}
		
		
		for(int i =0;i<num;i++){
			
				data.add(bytes[i]);
				
			}
          
			
			service.readMessageInfo(data);
	}




}
